package com.hxkcloud.nurse.activitys;

import android.os.Message;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hxkcloud.nurse.R;

public class WebListActivity extends BaseActivity implements View.OnClickListener
{
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private WebView webView;
    private ProgressBar pb_web;

    private String url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_list);

        initView();
        initListener();
        initData();
    }

    @Override
    protected void updateByHandler(Message msg) {

    }

    @Override
    protected void initView()
    {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText("脑梗死类型");

        webView = (WebView) findViewById(R.id.webview_article);
        pb_web = (ProgressBar) findViewById(R.id.pb_article);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); //支持js
        webSettings.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSettings.setSupportZoom(true);  //支持缩放
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);  //设置缓存模式
        webSettings.setDomStorageEnabled(true);

    }

    @Override
    protected void initListener()
    {
        url = getIntent().getStringExtra("url");

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                pb_web.setProgress(newProgress);
                if (newProgress == 100)
                {
                    pb_web.setVisibility(View.INVISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });


        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                if (url.contains("backInfo"))
                {
                    finish();
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }

        });
        webView.loadUrl(url);
    }

    @Override
    protected void initData()
    {


    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_title_back:
                setResult(RESULT_OK);
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            //TODO something
            setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
